﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SystemModule;

namespace M2Server
{
    public partial class TMerchant
    {
        // 保存升级武器列表
        private void SaveUpgradingList()
        {
            try
            {
                // LocalDB.FrmDB.SaveUpgradeWeaponRecord(m_sScript + "-" + this.m_sMapName, m_UpgradeWeaponList);
            }
            catch
            {
                M2Share.MainOutMessage("保存升级列表失败 - " + this.m_sCharName);
            }
        }

        public void UpgradeWapon_sub_4A0218(TPlayObject User, ref byte btDc, ref byte btSc, ref byte btMc, ref byte btDura)
        {
            int II;
            ArrayList DuraList;
            TUserItem UserItem;
            TStdItem StdItem;
            TStdItem StdItem80;
            TStringList DelItemList;
            int nDc;
            int nSc;
            int nMc;
            int nDcMin;
            int nDcMax;
            int nScMin;
            int nScMax;
            int nMcMin;
            int nMcMax;
            int nDura;
            int nItemCount;
            nDcMin = 0;
            nDcMax = 0;
            nScMin = 0;
            nScMax = 0;
            nMcMin = 0;
            nMcMax = 0;
            nDura = 0;
            nItemCount = 0;
            DelItemList = null;
            DuraList = new ArrayList();
            if (User.m_ItemList.Count > 0)
            {
                for (var i = User.m_ItemList.Count - 1; i >= 0; i--)
                {
                    UserItem = User.m_ItemList[i];
                    if (M2Share.UserEngine.GetStdItemName(UserItem.wIndex) == M2Share.g_Config.sBlackStone)
                    {
                        DuraList.Add((HUtil32.Round(UserItem.Dura / 1.0E3) as object));
                        if (DelItemList == null)
                        {
                            DelItemList = new TStringList();
                        }
                        //DelItemList.Add(M2Share.g_Config.sBlackStone, ((UserItem.MakeIndex) as Object));
                        UserItem = null;//UserItem=null;
                        User.m_ItemList.RemoveAt(i);
                    }
                    else
                    {
                        if (M2Share.IsUseItem(UserItem.wIndex))
                        {
                            StdItem = M2Share.UserEngine.GetStdItem(UserItem.wIndex);
                            if (StdItem != null)
                            {
                                StdItem80 = StdItem;
                                M2Share.ItemUnit.GetItemAddValue(UserItem, StdItem80);
                                nDc = 0;
                                nSc = 0;
                                nMc = 0;
                                switch (StdItem80.StdMode)
                                {
                                    case 19:
                                    case 20:
                                    case 21:
                                        nDc = HUtil32.HiWord(StdItem80.DC) + HUtil32.LoWord(StdItem80.DC);
                                        nSc = HUtil32.HiWord(StdItem80.SC) + HUtil32.LoWord(StdItem80.SC);
                                        nMc = HUtil32.HiWord(StdItem80.MC) + HUtil32.LoWord(StdItem80.MC);
                                        break;
                                    case 22:
                                    case 23:
                                        nDc = HUtil32.HiWord(StdItem80.DC) + HUtil32.LoWord(StdItem80.DC);
                                        nSc = HUtil32.HiWord(StdItem80.SC) + HUtil32.LoWord(StdItem80.SC);
                                        nMc = HUtil32.HiWord(StdItem80.MC) + HUtil32.LoWord(StdItem80.MC);
                                        break;
                                    case 24:
                                    case 26:
                                        nDc = HUtil32.HiWord(StdItem80.DC) + HUtil32.LoWord(StdItem80.DC) + 1;
                                        nSc = HUtil32.HiWord(StdItem80.SC) + HUtil32.LoWord(StdItem80.SC) + 1;
                                        nMc = HUtil32.HiWord(StdItem80.MC) + HUtil32.LoWord(StdItem80.MC) + 1;
                                        break;
                                }
                                if (nDcMin < nDc)
                                {
                                    nDcMax = nDcMin;
                                    nDcMin = nDc;
                                }
                                else
                                {
                                    if (nDcMax < nDc)
                                    {
                                        nDcMax = nDc;
                                    }
                                }
                                if (nScMin < nSc)
                                {
                                    nScMax = nScMin;
                                    nScMin = nSc;
                                }
                                else
                                {
                                    if (nScMax < nSc)
                                    {
                                        nScMax = nSc;
                                    }
                                }
                                if (nMcMin < nMc)
                                {
                                    nMcMax = nMcMin;
                                    nMcMin = nMc;
                                }
                                else
                                {
                                    if (nMcMax < nMc)
                                    {
                                        nMcMax = nMc;
                                    }
                                }
                                if (DelItemList == null)
                                {
                                    DelItemList = new TStringList();
                                }
                                //DelItemList.Add(StdItem.Name, ((UserItem.MakeIndex) as Object));
                                if (StdItem.NeedIdentify == 1)
                                {
                                    M2Share.AddGameDataLog("26" + "\09" + User.m_sMapName + "\09" + (User.m_nCurrX).ToString()
                                    + "\09" + (User.m_nCurrY).ToString() + "\09" + User.m_sCharName + "\09" + StdItem.Name + "\09"
                                        + (UserItem.MakeIndex).ToString() + "\09" + "(" + (HUtil32.LoWord(StdItem.DC)).ToString() + "/"
                                        + (HUtil32.HiWord(StdItem.DC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MC)).ToString()
                                        + "/" + (HUtil32.HiWord(StdItem.MC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.SC)).ToString()
                                        + "/" + (HUtil32.HiWord(StdItem.SC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.AC)).ToString()
                                        + "/" + (HUtil32.HiWord(StdItem.AC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MAC)).ToString()
                                        + "/" + (HUtil32.HiWord(StdItem.MAC)).ToString() + ")" + (UserItem.btValue[0]).ToString()
                                        + "/" + (UserItem.btValue[1]).ToString() + "/" + (UserItem.btValue[2]).ToString() + "/"
                                        + (UserItem.btValue[3]).ToString() + "/" + (UserItem.btValue[4]).ToString() + "/"
                                        + (UserItem.btValue[5]).ToString() + "/" + (UserItem.btValue[6]).ToString() + "/"
                                        + (UserItem.btValue[7]).ToString() + "/" + (UserItem.btValue[8]).ToString() + "/"
                                        + (UserItem.btValue[14]).ToString() + "\09" + this.m_sCharName);
                                }
                                //Marshal.FreeHGlobal(UserItem);
                                User.m_ItemList.RemoveAt(i);
                            }
                        }
                    }
                }
            }
            for (var i = 0; i < DuraList.Count; i++)
            {
                if (DuraList.Count <= 0)
                {
                    break;
                }
                for (II = DuraList.Count - 1; II > i; II--)
                {
                    if (((int)DuraList[II]) > ((int)DuraList[II - 1]))
                    {
                        //DuraList.Exchange(II, II - 1);
                    }
                }
            }
            if (DuraList.Count > 0)
            {
                for (var i = 0; i < DuraList.Count; i++)
                {
                    nDura = nDura + ((int)DuraList[i]);
                    nItemCount++;
                    if (nItemCount >= 5)
                    {
                        break;
                    }
                }
            }
            btDura = (byte)HUtil32.Round(HUtil32._MIN(5, nItemCount) + HUtil32._MIN(5, nItemCount) * ((nDura / nItemCount) / 5.0));
            btDc = Convert.ToByte(nDcMin / 5 + nDcMax / 3);
            btSc = Convert.ToByte(nScMin / 5 + nScMax / 3);
            btMc = Convert.ToByte(nMcMin / 5 + nMcMax / 3);
            if (DelItemList != null)
            {
                Console.WriteLine("todo RM_SENDDELITEMLIST...");
               // User.SendMsg(this, Grobal2.RM_SENDDELITEMLIST, 0, DelItemList.ToInt(), 0, 0, "");
            }
            if (DuraList != null)
            {
                Dispose(DuraList);
            }
        }

        /// <summary>
        /// 升级武器
        /// </summary>
        /// <param name="User"></param>
        private void UpgradeWapon(TPlayObject User)
        {
            bool bo0D = false;
            TUpgradeInfo UpgradeInfo;
            TStdItem StdItem;
            try
            {
                if (User.m_UseItems[Grobal2.U_WEAPON] == null)
                {
                    return;
                }
                if (m_UpgradeWeaponList.Count > 0)
                {
                    for (int i = 0; i < m_UpgradeWeaponList.Count; i++)
                    {
                        UpgradeInfo = m_UpgradeWeaponList[i];
                        if (UpgradeInfo.sUserName == User.m_sCharName)
                        {
                            this.GotoLable(User, M2Share.sUPGRADEING, false);
                            return;
                        }
                    }
                }
                if ((User.m_UseItems[Grobal2.U_WEAPON].wIndex != 0) && (User.m_nGold >= M2Share.g_Config.nUpgradeWeaponPrice) && (User.CheckItems(M2Share.g_Config.sBlackStone) != null))
                {
                    User.DecGold(M2Share.g_Config.nUpgradeWeaponPrice);
                    if (m_boCastle || M2Share.g_Config.boGetAllNpcTax)
                    {
                        if (this.m_Castle != null)
                        {
                            this.m_Castle.IncRateGold(M2Share.g_Config.nUpgradeWeaponPrice);
                        }
                        else if (M2Share.g_Config.boGetAllNpcTax)
                        {
                            M2Share.g_CastleManager.IncRateGold(M2Share.g_Config.nUpgradeWeaponPrice);
                        }
                    }
                    User.GoldChanged();
                    UpgradeInfo = new TUpgradeInfo();
                    UpgradeInfo.sUserName = User.m_sCharName;
                    UpgradeInfo.UserItem = User.m_UseItems[Grobal2.U_WEAPON];
                    StdItem = M2Share.UserEngine.GetStdItem(User.m_UseItems[Grobal2.U_WEAPON].wIndex);
                    if (StdItem.NeedIdentify == 1)
                    {
                        M2Share.AddGameDataLog("25" + "\09" + User.m_sMapName + "\09" + (User.m_nCurrX).ToString() + "\09" + (User.m_nCurrY).ToString()
                            + "\09" + User.m_sCharName + "\09" + StdItem.Name + "\09" + (User.m_UseItems[Grobal2.U_WEAPON].MakeIndex).ToString() + "\09"
                            + "(" + (HUtil32.LoWord(StdItem.DC)).ToString() + "/" + (HUtil32.HiWord(StdItem.DC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MC)).ToString()
                            + "/" + (HUtil32.HiWord(StdItem.MC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.SC)).ToString() + "/" + (HUtil32.HiWord(StdItem.SC)).ToString() +
                            ")" + "(" + (HUtil32.LoWord(StdItem.AC)).ToString() + "/" + (HUtil32.HiWord(StdItem.AC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MAC)).ToString()
                            + "/" + (HUtil32.HiWord(StdItem.MAC)).ToString() + ")" + (User.m_UseItems[Grobal2.U_WEAPON].btValue[0]).ToString() + "/"
                            + (User.m_UseItems[Grobal2.U_WEAPON].btValue[1]).ToString() + "/" + (User.m_UseItems[Grobal2.U_WEAPON].btValue[2]).ToString() + "/"
                            + (User.m_UseItems[Grobal2.U_WEAPON].btValue[3]).ToString() + "/" + (User.m_UseItems[Grobal2.U_WEAPON].btValue[4]).ToString() + "/"
                            + (User.m_UseItems[Grobal2.U_WEAPON].btValue[5]).ToString() + "/" + (User.m_UseItems[Grobal2.U_WEAPON].btValue[6]).ToString() + "/"
                            + (User.m_UseItems[Grobal2.U_WEAPON].btValue[7]).ToString() + "/" + (User.m_UseItems[Grobal2.U_WEAPON].btValue[8]).ToString() + "/"
                            + (User.m_UseItems[Grobal2.U_WEAPON].btValue[14]).ToString() + "\09" + (User.m_UseItems[Grobal2.U_WEAPON].Dura).ToString() + "/"
                            + (User.m_UseItems[Grobal2.U_WEAPON].DuraMax).ToString());
                    }
                    User.ClearCopyItem(0, User.m_UseItems[Grobal2.U_WEAPON].wIndex, User.m_UseItems[Grobal2.U_WEAPON].MakeIndex); // 清理包裹和仓库复制物品
                    User.SendDelItems(User.m_UseItems[Grobal2.U_WEAPON]);
                    User.m_UseItems[Grobal2.U_WEAPON].wIndex = 0;
                    User.RecalcAbilitys();
                    User.CompareSuitItem(false); //套装
                    User.FeatureChanged();
                    User.SendMsg(User, Grobal2.RM_ABILITY, 0, 0, 0, 0, "");
                    UpgradeWapon_sub_4A0218(User, ref UpgradeInfo.btDc, ref UpgradeInfo.btSc, ref UpgradeInfo.btMc, ref UpgradeInfo.btDura);
                    UpgradeInfo.dtTime = DateTime.Now;
                    UpgradeInfo.dwGetBackTick = (ushort)HUtil32.GetTickCount();
                    m_UpgradeWeaponList.Add(UpgradeInfo);
                    SaveUpgradingList();// 保存武器升级列表
                    bo0D = true;
                }
                if (bo0D)
                {
                    this.GotoLable(User, M2Share.sUPGRADEOK, false);
                }
                else
                {
                    this.GotoLable(User, M2Share.sUPGRADEFAIL, false);
                }
            }
            catch
            {
                M2Share.MainOutMessage("{异常} TMerchant.UpgradeWapon");
            }
        }

        /// <summary>
        /// 取回升级武器
        /// </summary>
        /// <param name="User"></param>
        private void GetBackupgWeapon(TPlayObject User)
        {
            TUpgradeInfo UpgradeInfo = null;
            int n10;
            int n18;
            int n1C;
            int n90;
            TUserItem UserItem;
            TStdItem StdItem;
            try
            {
                n18 = 0;
                if (!User.IsEnoughBag()) // 背包已经满
                {
                    this.GotoLable(User, M2Share.sGETBACKUPGFULL, false);
                    return;
                }
                for (int i = m_UpgradeWeaponList.Count - 1; i >= 0; i--)
                {
                    if (m_UpgradeWeaponList.Count <= 0)
                    {
                        break;
                    }
                    UpgradeInfo = m_UpgradeWeaponList[i];
                    if (UpgradeInfo.sUserName == User.m_sCharName)
                    {
                        n18 = 1;
                        if ((((HUtil32.GetTickCount() - UpgradeInfo.dwGetBackTick) > M2Share.g_Config.dwUPgradeWeaponGetBackTime) || (User.m_btPermission >= 4)))
                        {
                            UpgradeInfo = m_UpgradeWeaponList[i];
                            m_UpgradeWeaponList.RemoveAt(i);
                            SaveUpgradingList();// 保存武器升级列表
                            n18 = 2;
                            break;
                        }
                    }
                }
                if ((UpgradeInfo != null) && (n18 == 2))
                {
                    if (HUtil32.rangeInDefined(UpgradeInfo.btDura, 0, 8))
                    {
                        if (UpgradeInfo.UserItem.DuraMax > 3000)
                        {
                            UpgradeInfo.UserItem.DuraMax -= 3000;
                        }
                        else
                        {
                            UpgradeInfo.UserItem.DuraMax = Convert.ToUInt16(UpgradeInfo.UserItem.DuraMax >> 1);
                        }
                        if (UpgradeInfo.UserItem.Dura > UpgradeInfo.UserItem.DuraMax)
                        {
                            UpgradeInfo.UserItem.Dura = UpgradeInfo.UserItem.DuraMax;
                        }
                    }
                    if (HUtil32.rangeInDefined(UpgradeInfo.btDura, 9, 15))
                    {
                        if (HUtil32.Random(UpgradeInfo.btDura) < 6)
                        {
                            if (UpgradeInfo.UserItem.DuraMax > 1000)
                            {
                                UpgradeInfo.UserItem.DuraMax -= 1000;
                            }
                            if (UpgradeInfo.UserItem.Dura > UpgradeInfo.UserItem.DuraMax)
                            {
                                UpgradeInfo.UserItem.Dura = UpgradeInfo.UserItem.DuraMax;
                            }
                        }
                    }
                    if (HUtil32.rangeInDefined(UpgradeInfo.btDura, 18, 255))
                    {
                        if (HUtil32.rangeInDefined(HUtil32.Random(UpgradeInfo.btDura - 18), 1, 4))
                        {
                            UpgradeInfo.UserItem.DuraMax += 1000;
                        }
                        if (HUtil32.rangeInDefined(HUtil32.Random(UpgradeInfo.btDura - 18), 5, 7))
                        {
                            UpgradeInfo.UserItem.DuraMax += 2000;
                        }
                        if (HUtil32.rangeInDefined(HUtil32.Random(UpgradeInfo.btDura - 18), 8, 255))
                        {
                            UpgradeInfo.UserItem.DuraMax += 4000;
                        }
                    }
                    if ((UpgradeInfo.btDc == UpgradeInfo.btMc) && (UpgradeInfo.btMc == UpgradeInfo.btSc))
                    {
                        n1C = HUtil32.Random(3);
                    }
                    else
                    {
                        n1C = -1;
                    }
                    if (((UpgradeInfo.btDc >= UpgradeInfo.btMc) && (UpgradeInfo.btDc >= UpgradeInfo.btSc)) || (n1C == 0))
                    {
                        n90 = HUtil32._MIN(11, UpgradeInfo.btDc);
                        n10 = HUtil32._MIN(85, n90 << 3 - n90 + 10 + UpgradeInfo.UserItem.btValue[3] - UpgradeInfo.UserItem.btValue[4] + User.m_nBodyLuckLevel);
                        if (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponDCRate) < n10)
                        {
                            UpgradeInfo.UserItem.btValue[10] = 10;
                            if ((n10 > 63) && (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponDCTwoPointRate) == 0))
                            {
                                UpgradeInfo.UserItem.btValue[10] = 11;
                            }
                            if ((n10 > 79) && (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponDCThreePointRate) == 0))
                            {
                                UpgradeInfo.UserItem.btValue[10] = 12;
                            }
                        }
                        else
                        {
                            UpgradeInfo.UserItem.btValue[10] = 1; // 等于1武器破碎
                        }
                    }
                    if (((UpgradeInfo.btMc >= UpgradeInfo.btDc) && (UpgradeInfo.btMc >= UpgradeInfo.btSc)) || (n1C == 1))
                    {
                        n90 = HUtil32._MIN(11, UpgradeInfo.btMc);
                        n10 = HUtil32._MIN(85, n90 << 3 - n90 + 10 + UpgradeInfo.UserItem.btValue[3] - UpgradeInfo.UserItem.btValue[4] + User.m_nBodyLuckLevel);
                        if (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponMCRate) < n10)
                        {
                            UpgradeInfo.UserItem.btValue[10] = 20;
                            if ((n10 > 63) && (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponMCTwoPointRate) == 0))
                            {
                                UpgradeInfo.UserItem.btValue[10] = 21;
                            }
                            if ((n10 > 79) && (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponMCThreePointRate) == 0))
                            {
                                UpgradeInfo.UserItem.btValue[10] = 22;
                            }
                        }
                        else
                        {
                            UpgradeInfo.UserItem.btValue[10] = 1; // 等于1武器破碎
                        }
                    }
                    if (((UpgradeInfo.btSc >= UpgradeInfo.btMc) && (UpgradeInfo.btSc >= UpgradeInfo.btDc)) || (n1C == 2))
                    {
                        n90 = HUtil32._MIN(11, UpgradeInfo.btMc);
                        n10 = HUtil32._MIN(85, n90 << 3 - n90 + 10 + UpgradeInfo.UserItem.btValue[3] - UpgradeInfo.UserItem.btValue[4] + User.m_nBodyLuckLevel);
                        if (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponSCRate) < n10)
                        {
                            UpgradeInfo.UserItem.btValue[10] = 30;
                            if ((n10 > 63) && (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponSCTwoPointRate) == 0))
                            {
                                UpgradeInfo.UserItem.btValue[10] = 31;
                            }
                            if ((n10 > 79) && (HUtil32.Random(M2Share.g_Config.nUpgradeWeaponSCThreePointRate) == 0))
                            {
                                UpgradeInfo.UserItem.btValue[10] = 32;
                            }
                        }
                        else
                        {
                            UpgradeInfo.UserItem.btValue[10] = 1;   // 等于1武器破碎
                        }
                    }
                    UserItem = UpgradeInfo.UserItem;
                    UserItem.btValue[9] = Convert.ToByte(HUtil32._MIN(255, UserItem.btValue[9] + 1));  // 累积升级次数
                    StdItem = M2Share.UserEngine.GetStdItem(UserItem.wIndex);
                    if (StdItem.NeedIdentify == 1)
                    {
                        M2Share.AddGameDataLog("24" + "\09" + User.m_sMapName + "\09" + (User.m_nCurrX).ToString() + "\09" + (User.m_nCurrY).ToString() + "\09" + User.m_sCharName + "\09" + StdItem.Name
                            + "\09" + (UserItem.MakeIndex).ToString() + "\09" + "(" + (HUtil32.LoWord(StdItem.DC)).ToString() + "/" + (HUtil32.HiWord(StdItem.DC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MC)).ToString()
                            + "/" + (HUtil32.HiWord(StdItem.MC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.SC)).ToString() + "/" + (HUtil32.HiWord(StdItem.SC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.AC)).ToString()
                            + "/" + (HUtil32.HiWord(StdItem.AC)).ToString() + ")" + "(" + (HUtil32.LoWord(StdItem.MAC)).ToString() + "/" + (HUtil32.HiWord(StdItem.MAC)).ToString() + ")" + (UserItem.btValue[0]).ToString()
                            + "/" + (UserItem.btValue[1]).ToString() + "/" + (UserItem.btValue[2]).ToString() + "/" + (UserItem.btValue[3]).ToString() + "/" + (UserItem.btValue[4]).ToString()
                            + "/" + (UserItem.btValue[5]).ToString() + "/" + (UserItem.btValue[6]).ToString() + "/" + (UserItem.btValue[7]).ToString() + "/" + (UserItem.btValue[8]).ToString()
                            + "/" + (UserItem.btValue[14]).ToString() + "\09" + (UserItem.Dura).ToString() + "/" + (UserItem.DuraMax).ToString());
                    }
                    User.ClearCopyItem(0, UserItem.wIndex, UserItem.MakeIndex); // 清理包裹和仓库复制物品
                    User.AddItemToBag(UserItem);
                    User.SendAddItem(UserItem);
                    UpgradeInfo = null;
                }
                switch (n18)
                {
                    case 0:
                        this.GotoLable(User, M2Share.sGETBACKUPGFAIL, false);
                        break;
                    case 1:
                        this.GotoLable(User, M2Share.sGETBACKUPGING, false);
                        break;
                    case 2:
                        this.GotoLable(User, M2Share.sGETBACKUPGOK, false);
                        break;
                }
            }
            catch
            {
                M2Share.MainOutMessage("{异常} TMerchant.GetBackupgWeapon");
            }
        }
    }
}
